<extend name="Base/ucenter_common"/>
<block name="body">
    <div id="zj-bh-search">
        <style>
            .form {
                margin-top: 30px;
            }

        </style>
        <div class="widget-box">
            <div class="widget-header">
                <h3>认证中心：</h3>
            </div>
            <div class="widget-body">
                <eq name="_user.real_status" value="0">
                    <eq name="_user.u_type" value="2">
                        <span>选择认证类型<small style="color: red;">不可修改</small>:</span>
                        <a data-value="1" class="btn action-auth btn-lg btn-success">个人认证</a>
                        <a data-value="0" class="btn action-auth btn-lg btn-primary">机构认证</a>
                    </eq>
                    <eq name="_user.u_type" value="1">
                        <!-- 个人认证部分 -->
                        <div class="col-md-5 col-md-offset-3">
                            <form id="user_auth_form" action="{:U('user/do_auth')}" method="post"
                                  enctype="multipart/form-data">
                                <div class="form-group">
                                    <label class="control-label">身份证号<span style="color: red;"
                                                                           v-html="auth_tip"></span></label>
                                    <input class="form-control" type="text" placeholder="请输入身份证号" maxlength="20"
                                           name="idcard" v-model="user.idcard" @keyup="searchAuth();">
                                </div>
                                <div class="form-group">
                                    <label class="control-label">姓名<span style="color: red;"
                                                                         v-html="auth_name_tip"></span></label>
                                    <input class="form-control" type="text"
                                           placeholder="" maxlength="15"
                                           name="realname" v-model="user.realname">
                                </div>
                                <div class="form-group">
                                    <label class="control-label">性别</label>
                                    <input type="text" readonly="readonly"
                                           name="u_sex" class="form-control"
                                           v-model="user.sex">
                                </div>
                                <div class="form-group">
                                    <label class="control-label">年龄</label>
                                    <input class="form-control" type="text" placeholder="" maxlength="4"
                                           name="u_age" v-model="user.age">
                                </div>
                                <div class="form-group">
                                    <label class="control-label">身份证照片</label>
                                    <div>
                                        <eq name="_user.u_id_url" value="0">
                                            <span>亲，你还没有上传图片哦</span>
                                            <else/>
                                            <img src="__PUBLIC__/image/{$person_url}" style="width: 100px;"/>
                                        </eq>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <input type="file" name="user_photo" value="">
                                </div>
                                <div class="form-group text-center">
                                    <button type="button" @click="do_auth_User" class="btn btn-block btn-info">提交认证
                                    </button>
                                </div>
                            </form>
                        </div>
                    </eq>
                    <eq name="_user.u_type" value="0">
                        <!-- 机构认证部分 -->
                        <div class="col-md-5 col-md-offset-3">
                            <form action="{:U('user/do_auth')}" method="post" enctype="multipart/form-data">
                                <div class="form-group">
                                    <label class="control-label">机构名称<span style="color: red;">*</span></label>
                                    <input class="form-control" type="text"
                                           required="required"
                                           placeholder="请输入机构名称"
                                           name="realname">
                                </div>
                                <div class="form-group">
                                    <label class="control-label">机构代码<span style="color: red;">*</span></label>
                                    <input class="form-control" type="text"
                                           placeholder="请输入机构代码"
                                           required="required"
                                           name="idcard">
                                </div>
                                <div class="form-group">
                                    <label class="control-label">机构证件图</label>
                                    <div>
                                        <eq name="_user.u_id_url" value="0">
                                            <span>亲，你还没有上传图片哦</span>
                                            <else/>
                                            <img src="__PUBLIC__/image/{$_user.u_id_url}" style="width: 100px;"/>
                                        </eq>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <input type="file" name="user_photo" value="">
                                </div>
                                <div class="form-group text-center">
                                    <button type="submit" class="btn btn-block btn-info">提交认证</button>
                                </div>
                            </form>
                        </div>
                    </eq>
                    <else/>

                    <table class="table table-bordered">
                        <thead>

                        </thead>
                        <tbody>
                        <tr>
                            <td>是否认证</td>
                            <td>
                                <label class="label label-success">已经认证</label>
                            </td>
                        </tr>
                        <tr>
                            <td>认证时间</td>
                            <td>

                            </td>
                        </tr>
                        <tr>
                            <td>认证类型</td>
                            <td>

                            </td>
                        </tr>
                        </tbody>
                    </table>
                </eq>
            </div>
        </div>
    </div>
    <script>
        var auth_type = parseInt("{$_user.u_type}");
        new Vue({
            el: '#zj-bh-search',
            data: {
                user: {realname: '', idcard: '', sex: '', age: ''},
                auth_pass: false,
                auth_tip: '',
                auth_name_tip: ''
            },
            methods: {
                searchAuth: function () {
                    var id_card = this.$data.user.idcard;
                    if (id_card.length != 18) {
                        this.$data.auth_tip = "请输入正确的身份证号码*";
                        return;
                    }
                    this.$data.auth_tip = '';
                },
                intiAuthData: function (opt, callback) {
                    //初始化后台用户数据
                    var id_card = opt.$data.user.idcard;
                    if (id_card.length != 18) {
                        opt.$data.auth_tip = "请输入正确的身份证号码*";
                        return;
                    }
                    if (opt.$data.user.realname.length <= 1) {
                        opt.$data.auth_name_tip = "请输入正确的用户名*";
                        return;
                    }
                    $.post("{:U('User/search_auth')}", {
                        id_card: id_card,
                        real_name: opt.$data.user.realname
                    }, function (ret) {
                        callback(ret);
                    });
                },
                do_auth_User: function () {
                    var _self = this;
                    this.$options.methods.intiAuthData(this, function (ret) {
                        if (ret.status != 1) {
                            layer.msg(ret.info);
                            return;
                        }
                        //赋值
                        if (ret.data.err == -1) {
                            layer.msg("不符合身份");
                            return;
                        }
                        _self.$data.user.sex = ret.data.sex;
                        //计算年龄
                        _self.$data.user.age = ages(ret.data.birthday);
                        $("#user_auth_form").submit();
                    });
                },
                do_auth_Origin: function () {

                }
            }
        });
        $(function () {
            $(".action-auth").bind('click', function (e) {
                e.preventDefault();
                if (!window.confirm('您确认使用这种用户类型么？确认后将不可修改')) {
                    return;
                }
                var authType = $(this).attr('data-value');
                $.post("{:U('User/choose_auth')}", {auth_type: authType}, function (ret) {
                    window.location.reload();
                });
            });
        });

        function ages(str) {
            var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
            if (r == null)return false;
            var d = new Date(r[1], r[3] - 1, r[4]);
            if (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4]) {
                var Y = new Date().getFullYear();
                return Y - r[1];
            }
            return ("输入的日期格式错误！");
        }

        function getSex(str) {
            if (str == 'M') {
                return '男';
            } else if (str == 'F') {
                return '女';
            } else {
                return '未知';
            }

        }
    </script>
</block>